﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;

namespace Infrastructure.CompactCE.ExceptionHandling
{
    public class ExceptionPolicy
    {
        
        public static bool HandleException(Exception ex, ExceptionPolicyNames PolicyName)
        {
            bool result = true;

            StringBuilder sb = new StringBuilder();  

            string logname = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            logname = logname + "\\Infrastructure.log";
            
            var fs = File.CreateText(logname);  
            
            
            switch (PolicyName)
            {
                case ExceptionPolicyNames.ServiceAccessPolicy:
                    sb.AppendLine("ServiceAccessPolicy");
                    sb.AppendLine(ex.Message);
                    sb.AppendLine(ex.StackTrace);
                    fs.Write(sb.ToString()); 
                    break;
                case ExceptionPolicyNames.LocalDataAccessPolicy:
                    sb.AppendLine("LocalDataAccessPolicy");
                    sb.AppendLine(ex.Message);
                    sb.AppendLine(ex.StackTrace);
                    fs.Write(sb.ToString()); 
                    break;
                case ExceptionPolicyNames.RemoteDataAccessPolicy:
                    sb.AppendLine("RemoteDataAccessPolicy");
                    sb.AppendLine(ex.Message);
                    sb.AppendLine(ex.StackTrace);
                    fs.Write(sb.ToString()); 
                    break;
                case ExceptionPolicyNames.SmartViewPolicy:
                    sb.AppendLine("SmartViewPolicy");
                    sb.AppendLine(ex.Message);
                    sb.AppendLine(ex.StackTrace);
                    fs.Write(sb.ToString());
                    System.Windows.Forms.MessageBox.Show(ex.Message,  "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation, System.Windows.Forms.MessageBoxDefaultButton.Button1);
                    result = false; 
                    break;
                case ExceptionPolicyNames.ServicePolicy:
                    sb.AppendLine("ServicePolicy");
                    sb.AppendLine(ex.Message);
                    sb.AppendLine(ex.StackTrace);
                    fs.Write(sb.ToString()); 
                    break;
                default:
                    break;
            }
            fs.Flush();
            fs.Close(); 
            return result; 
        }
    }
}
